﻿using AnesSystem.Models;
using AnesSystem.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace AnesSystem.ViewModels
{
    public class ContinueEventEditViewModel : BaseViewModel<ContinueEventEditViewModel>
    {

        #region Variables
        private PatientEventsModel _patientEvent;
        private bool _isEnd;
        private Visibility _isShowEndTime;

        #endregion


        #region Constructor

        #endregion


        #region Properties

        public PatientEventsModel PatientEvent
        {
            get
            {
                return _patientEvent;
            }

            set
            {
                _patientEvent = value;
                ChangeIsEnd();
                RaisePropertyChanged("PatientEvent");
            }
        }


        public bool IsEnd
        {
            get
            {
                return _isEnd;
            }

            set
            {
                _isEnd = value;
                RaisePropertyChanged("IsEnd");
            }
        }

        public Visibility IsShowEndTime
        {
            get
            {
                return _isShowEndTime;
            }

            set
            {
                _isShowEndTime = value;
                RaisePropertyChanged("IsShowEndTime");
            }
        }








        #endregion


        #region 依赖属性


        #endregion


        #region Event


        #endregion


        #region PrivateMethods
        private void ChangeIsEnd()
        {

            if (PatientEvent.EndTime.Equals(new DateTime()))
            {
                IsEnd = false;
                IsShowEndTime = Visibility.Hidden;
            }
            else
            {
                IsEnd = true;
                IsShowEndTime = Visibility.Visible;
            }
        }

        private bool CheckEnterValue()
        {
            DateTime inRoomTime = DataManager.Instance.CurrentPatientLink.Status.InRoomTime;
            DateTime outRoomTime = DataManager.Instance.CurrentPatientLink.Status.OutRoomTime;
            ConfigEventsModel cem = null;
            if (DataManager.Instance.Code2events.ContainsKey(PatientEvent.Code))
            {
                cem = DataManager.Instance.Code2events[PatientEvent.Code];
            }
            if (cem == null)
            {
                return false;
            }
            if (!PatientEvent.EndTime.Equals(new DateTime()) &&PatientEvent.EndTime < PatientEvent.BeginTime)
            {
                MessageBoxCommon.ShowDialog("结束时间应大于开始时间!", MessageBoxCommon.OK);
                return false;
            }
            if (!inRoomTime.Equals(new DateTime()))
            {
                if (PatientEvent.BeginTime < inRoomTime && !cem.Name.Equals("入手术室"))
                {
                    MessageBoxCommon.ShowDialog("开始时间应大于入室时间!", MessageBoxCommon.OK);
                    return false;
                }
                if (!PatientEvent.EndTime.Equals(new DateTime()))
                {
                    if (PatientEvent.EndTime<inRoomTime)
                    {
                        MessageBoxCommon.ShowDialog("结束时间应大于入室时间!", MessageBoxCommon.OK);
                        return false;
                    }
                }
            }
            if (!outRoomTime.Equals(new DateTime()))
            {
                if (PatientEvent.BeginTime > outRoomTime && !cem.Name.Equals("入手术室") || PatientEvent.EndTime > outRoomTime && !cem.Name.Equals("入手术室"))
                {
                    MessageBoxCommon.ShowDialog("时间应小于入室时间!", MessageBoxCommon.OK);
                    return false;
                }
            }
            if (cem.Name.Equals("入手术室"))
            {
                if ((PatientEvent.EndTime - PatientEvent.BeginTime).TotalMinutes > 24 * 60)
                {
                    MessageBoxCommon.ShowDialog("手术时长应小于24小时!", MessageBoxCommon.OK);
                    return false;
                }
            }
            return true;

        }

        private void ChangeEventTime()
        {
            string CurEventCode = PatientEvent.Code;
            if (CurEventCode.Equals(Constants.EVENT_IN_ROOM))
            {
                DataManager.Instance.CurrentPatientLink.Status.InRoomTime = PatientEvent.BeginTime;
                DateTime inRoomTime = PatientEvent.BeginTime;
                DateTime anesRecordStartTime = inRoomTime.AddMinutes(-inRoomTime.Minute % 15);
                DataManager.Instance.CurrentPatientLink.Status.AnesRecordStartTime = anesRecordStartTime;
                DataManager.Instance.CurrentPatientLink.Status.OutRoomTime = PatientEvent.EndTime;
            }
            else if (CurEventCode.Equals(Constants.EVENT_ANES))
            {
                DataManager.Instance.CurrentPatientLink.Status.MazuiStartTime = PatientEvent.BeginTime;
                DataManager.Instance.CurrentPatientLink.Status.MazuiEndTime = PatientEvent.EndTime;
            }
            else if (CurEventCode.Equals(Constants.EVENT_OPERATION))
            {
                DataManager.Instance.CurrentPatientLink.Status.OperStartTime = PatientEvent.BeginTime;
                DataManager.Instance.CurrentPatientLink.Status.OperEndTime = PatientEvent.EndTime;
            }
            else if (CurEventCode.Equals(Constants.EVENT_CTRL_REST))
            {
                OperationDataPool.Instance.CurOperationData.VitalSignDataCache.RefreshRespEvent();
            }
            else if (CurEventCode.Equals(Constants.EVENT_SUB_REST))
            {
                OperationDataPool.Instance.CurOperationData.VitalSignDataCache.RefreshRespEvent();
            }
            SaveOper2DB();
            if (PatientEvent.EndTime.Equals(new DateTime()))
            {
                DataManager.Instance.TableHeaderCtrl._anesProcessCtrl.ViewModel.ChangeEvent(CurEventCode, false);
            }
            else
            {
                DataManager.Instance.TableHeaderCtrl._anesProcessCtrl.ViewModel.ChangeEvent(CurEventCode, true);
            }

        }

        private void SaveOper2DB()
        {
            PatientLinkService.Instance.AddUpdate(DataManager.Instance.CurrentPatientLink);
        }

        private void Save2DB()
        {
            PatientEventsService.Instance.AddUpdate(PatientEvent);
        }

        private void ChangeStatus(string code)
        {
            if (code.Equals(Constants.EVENT_ANES))
            {
                DataManager.Instance.CurrentPatientLink.Status.MazuiStartTime = new DateTime();
                DataManager.Instance.CurrentPatientLink.Status.MazuiEndTime = new DateTime();
                DataManager.Instance.CurrentPatientLink.Status.MazuiSpanTime = 0;
                SaveOper2DB();
            }
            if (code.Equals(Constants.EVENT_OPERATION))
            {
                DataManager.Instance.CurrentPatientLink.Status.OperStartTime = new DateTime();
                DataManager.Instance.CurrentPatientLink.Status.OperEndTime = new DateTime();
                DataManager.Instance.CurrentPatientLink.Status.OperSpanTime = 0;
                SaveOper2DB();
            }

        }




        #endregion


        #region Public/ProtectedMethods
        public string DelEvent()
        {
            {
                string code = PatientEvent.Code;

                ConfigEventsModel cem = null;
                if (DataManager.Instance.Code2events.ContainsKey(code))
                {
                    cem = DataManager.Instance.Code2events[code];
                }
                if (cem == null)
                {
                    return Constants.FAILED;
                }
                if (code.Equals(Constants.EVENT_IN_ROOM))
                {
                    if (cem.Name.Equals("入手术室"))
                    {
                        MessageBoxCommon.ShowDialog("入手术室事件不能删除!", MessageBoxCommon.OK);
                        return Constants.FAILED;
                    }
                }
                if (code.Equals(Constants.EVENT_ANES) || code.Equals(Constants.EVENT_OPERATION))
                {
                    OperationDataPool.Instance.CurOperationData.EventDataCache.DelEvent(PatientEvent);
                    ChangeStatus(code);
                }
                if (code.Equals(Constants.EVENT_CTRL_REST) || code.Equals(Constants.EVENT_SUB_REST))
                {
                    OperationDataPool.Instance.CurOperationData.EventDataCache.DelEvent(PatientEvent);
                    OperationDataPool.Instance.CurOperationData.VitalSignDataCache.RefreshRespEvent();
                }

                DataManager.Instance.TableHeaderCtrl._anesProcessCtrl.ViewModel.ChangeEvent(code, true);
                return Constants.SUCCESS;
            }
        }

        public void RefreshWin()
        {
            //刷新事件一栏
            for (int i = 1; i <= DataManager.Instance.CurrentPatientLink.Status.TotalPage; i++)
            {
                (DataManager.Instance.AnesRecordCtrl.DataContext as AnesRecordCtrlViewModel).AnesRecordCtrlViewModels[i].AnesEventAndMedicineCtrlViewModel.Refresh();
            }
            //刷新标记一栏
            for (int i = 1; i <= DataManager.Instance.CurrentPatientLink.Status.TotalPage; i++)
            {
                (DataManager.Instance.AnesRecordCtrl.DataContext as AnesRecordCtrlViewModel).AnesRecordCtrlViewModels[i].OperStatusItemCtrlViewModel.Refresh();
            }
            OperationDetailsViewModel.Instance.Refresh();
        }

        public string Save()
        {
            string status = Constants.FAILED;
            if (CheckEnterValue())
            {
                Save2DB();
                OperationDataPool.Instance.CurOperationData.EventDataCache.AddUpdateEvent(PatientEvent);
                ChangeEventTime();
                status = Constants.SUCCESS;
            }
            return status;
        }
        public void UpdateEndTime(bool isChecked)
        {
            if (isChecked)
            {
                PatientEvent.EndTime = DateTime.Now;
                IsEnd = true;
                IsShowEndTime = Visibility.Visible;
            }
            else
            {
                PatientEvent.EndTime = new DateTime();
                IsEnd = false;
                IsShowEndTime = Visibility.Hidden;
            }
            OperationDataPool.Instance.CurOperationData.EventDataCache.AddUpdateEvent(PatientEvent);
            ChangeEventTime();
        }
        #endregion


        #region EventHandlers


        #endregion
    }
}

